import 'package:flutter/material.dart';

class ErrorHintTextField extends StatefulWidget {
  static const TextStyle _DefaultTextStyle =
      TextStyle(fontSize: 14, color: Color(0xFF494B4D));
  static const TextStyle _DefaultErrorHintStyle =
      TextStyle(fontSize: 12, color: Color(0xFFF04B4B));
  static const TextStyle _DefaultHintStyle =
      TextStyle(fontSize: 14, color: Color(0xFFA0A4A7));

  final int maxLines;
  final int maxLength;
  final TextEditingController controller;
  final TextInputType keyboardType;
  final Function(String) onChanged;
  final bool obscureText;
  final TextStyle style;
  final TextStyle hintStyle;
  final String hintText;
  final Color underLineErrorColor;
  final Color underLineDefaultColor;
  final Color underLineActiveColor;
  final Color errorIconColor;
  final String errorHintText;
  final bool hasError;
  final TextStyle errorHintStyle;
  final Widget additionalWidget;
  final bool additionalWidgetVisible;

  ErrorHintTextField(
      {this.style = _DefaultTextStyle,
      this.hintStyle = _DefaultHintStyle,
      this.maxLines = 1,
      this.maxLength,
      this.controller,
      this.keyboardType,
      this.onChanged,
      this.obscureText = false,
      @required this.hintText,
      this.underLineDefaultColor = const Color(0xFFE2E5E7),
      this.underLineActiveColor = const Color(0x77A2C14D),
      this.underLineErrorColor = const Color(0xFFF04B4B),
      this.errorHintStyle = _DefaultErrorHintStyle,
      @required this.errorHintText,
      this.hasError,
      this.errorIconColor = const Color(0xFFF04B4B),
      this.additionalWidget,
      this.additionalWidgetVisible});

  @override
  State<StatefulWidget> createState() => _ErrorHintTextFieldState();
}

class _ErrorHintTextFieldState extends State<ErrorHintTextField> {
  FocusNode _focusNode;
  bool _focused = false;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode();
    _focusNode.addListener(() {
      setState(() {
        _focused = _focusNode.hasFocus;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Row(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: TextField(
                focusNode: _focusNode,
                controller: widget.controller,
                keyboardType: widget.keyboardType,
                maxLines: widget.maxLines,
                maxLength: widget.maxLength,
                onChanged: widget.onChanged,
                obscureText: widget.obscureText,
                style: widget.style,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  counterText: "",
                  hintText: widget.hintText,
                  hintStyle: widget.hintStyle,
                ),
              ),
            ),
            Visibility(
              visible: widget.hasError,
              child: Icon(
                Icons.error,
                size: 17,
                color: widget.errorIconColor,
              ),
            ),
            if (widget.additionalWidget != null)
              SizedBox(
                width: 10,
                height: 1,
              ),
            if (widget.additionalWidget != null)
              Visibility(
                visible: widget.additionalWidgetVisible,
                child: widget.additionalWidget,
              ),
          ],
        ),
        Divider(
          height: 1,
          thickness: 1,
          color: widget.hasError
              ? widget.underLineErrorColor
              : _focused
                  ? widget.underLineActiveColor
                  : widget.underLineDefaultColor,
        ),
        Text(
          widget.hasError ? widget.errorHintText : "",
          style: widget.errorHintStyle,
          textScaleFactor: 1,
        ),
      ],
    );
  }
}
